//
//  HttpRequest.m
//  OCDemo
//
//  Created by BH on 15/10/16.
//  Copyright © 2015年 baohan. All rights reserved.
//

#import "HttpRequest.h"
#import "RefreshModel.h"
#import <AFNetworking/AFNetworking.h>

#define EQUAL_STRING(o,d) [(o) isEqualToString:(d)]

@interface HttpRequest ()<NSXMLParserDelegate>
{
    NSMutableArray *arrayM;
    NSMutableString *elementStr;
    RefreshModel *model;
}

@end

@implementation HttpRequest

-(instancetype)initWithPage:(NSInteger)page delegate:(id)delegate
{
    if (self = [super init]) {
        _delegate = delegate;
        NSString *path = [NSString stringWithFormat:@"http://mps.manzuo.com/mps/cate?sid=(null)&id=0&cc=beijing&pt=all&ffst=%ld&mnt=%d&st=-1&hs=1",page,21];
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        [manager POST:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSXMLParser *parser = [[NSXMLParser alloc] initWithData:responseObject];
            parser.delegate = self;
            [parser parse];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error.localizedDescription);
        }];
    }
    
    return self;
}

#pragma mark - NSXMLParser delegate
//解析文档这里做初始化的工作
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    arrayM = [[NSMutableArray alloc] init];
    elementStr = [[NSMutableString alloc] init];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
    if ([elementName isEqualToString:@"promotion"]) {
        model = [[RefreshModel alloc] init];
    }
    
    [elementStr setString:@""];
}

//循环查找节点的内容
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [elementStr appendString:string];
}

//一个节点完成
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSString *result = [NSString stringWithString:elementStr];
    
    if ([elementName isEqualToString:@"promotion"]) {
        [arrayM addObject:model];
    }else if ([elementName isEqualToString:@"name"]){
        model.name = result;
    }else if ([elementName isEqualToString:@"price"]){
        model.price = result;
    }else if ([elementName isEqualToString:@"wsdimg"]){
        model.wsding = result;
    }else if ([elementName isEqualToString:@"district"]){
        model.district = result;
    }else if ([elementName isEqualToString:@"starttime"]){
        model.starttime = result;
    }else if ([elementName isEqualToString:@"endtime"]){
        model.endtime = result;
    }else if (EQUAL_STRING(elementName,@"multipagetitle")){
        model.multipagetitle = result;
    }else if (EQUAL_STRING(elementName,@"priceoff")){
        model.priceoff = result;
    }
}

//解析完成，处理数据
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    if ([_delegate respondsToSelector:@selector(request:array:)]) {
        [_delegate request:self array:arrayM];
    }
}

//解析出错，清理数据
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    [arrayM removeAllObjects];
}

@end
